<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "0");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网络服务平台</title>
<link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css">
<script type="text/javascript" src="/js/easyui/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function () {
    $('#newsGrid').datagrid({
        title: '新闻列表',
        iconCls: 'icon-save',
        width: "100%",
        height: 460,
        nowrap: false,
        striped: true,
        collapsible: true,
        url: '/management/news/fetchNews.action',
        queryParams: {"inIndex": "-1", "indexLevel": "-1"},
        remoteSort: false,
        idField: 'id',
        frozenColumns: [
            [
                /*   {field:'ck', checkbox:true},*/
                {"width": 350, "field": "title", "title": "标题",
                    formatter: function (value, rowData, rowIndex) {
                        if (!rowData.published) {
                            return '<a target="_blank" href="/management/news/showNewsBackend.action?id=' + rowData.id + '">' + value + '</a>';
                        } else {
                            return '<a target="_blank" href="/n-' + rowData.id + '.html">' + value + '</a>';
                        }
                    }
                }
            ]
        ],
        columns: [
            [
                {"width": 220, "field": "indexTitle", "title": "首页标题"},

                {"width": 70, "field": "inIndex", "title": "置顶级别",
                    formatter: function (value, rowData, rowIndex) {
                        if (value == '1') {
                            return '频道';
                        } else if (value == '2') {
                            return '主页';
                        }

                        return '无';
                    }
                },
                {"width": 70, "field": "indexLevel", "title": "推荐级别",
                    formatter: function (value, rowData, rowIndex) {
                        if (value == '1') {
                            return '推荐1';
                        } else if (value == '2') {
                            return '推荐2';
                        } else if (value == '3') {
                            return '推荐3';
                        } else if (value == '4') {
                            return '推荐4';
                        }

                        return '推荐5';
                    }
                },
                {"width": 30, "field": "published", "title": "发布",
                    formatter: function (value, rowData, rowIndex) {
                        if (value == '1') {
                            return '是';
                        } else if (value == '0') {
                            return '否';
                        }

                        return '否';
                    }
                },
                {"width": 130, "field": "createDate", "title": "创建日期"},
                {"width": 180, "field": "id", "title": "操作",
                    formatter: function (value, rowData, rowIndex) {
                        <s:if test="1 == #session._user_role_">
                        return '<a href="newsForm.action?id=' + value + ' ">编辑</a>&nbsp;<a onclick="deleteNews(\'' + value +
                                '\',\'' + rowIndex + '\');" href="#">删除</a>&nbsp;<a onclick="deleteHtml(\'' + value +
                                '\')" href="#">删除静态</a>&nbsp;<a onclick="saveHtml(\'' + value + '\')" href="#">静态</a>&nbsp;<a onclick="copyNews(\'' + value + '\')" href="#">拷贝</a>';
                        </s:if>
                        <s:elseif test="3 == #session._user_role_ || 4  == #session._user_role_">
                        return '';
                        </s:elseif>
                        <s:else>
                        return '<a href="newsForm.action?id=' + value + ' ">编辑</a>';
                        </s:else>
                    }
                }
            ]
        ],
        pagination: true,
        rownumbers: true,
        toolbar: [
            {"iconCls": "icon-add", "handler": function () {
                openCreateNews();
            }, "text": "添加新闻", "title": "添加新闻", "id": "btnAdd"}
            <s:if test="1 == #session._user_role_">
            ,
            {"iconCls": "icon-remove", "handler": function () {
                showMonthSelection();
            }, "text": "批量更新静态新闻", "title": "批量更新最新3个月新闻", "id": "btnOpe"}
            </s:if>
        ]
    });

    $('#indexLevel').combobox('setValue', -1);
    $('#inIndex').combobox('setValue', -1);

    var p = $('#newsGrid').datagrid('getPager');
    if (p) {
        $(p).pagination({
            onBeforeRefresh: function () {
                //	alert('before refresh');
            }
        });
    }
});

function openCreateNews() {
    window.location.href = "newsForm.action";
}

function refreshHtmlNews() {
   var month = $('input[name=monthSelection]').val();
    $.messager.confirm('批量更新新闻', '你确定要批量更新最近 '+month+' 个月静态新闻吗？', function (data) {
        if (!data) {
            return;
        }

        closeMonthSelection();
        $.messager.progress({
            title: '批量更新新闻',
            msg: '批量更新新闻......！'
        });

        $.getJSON("/management/news/refreshLatestNews.action?month="+month + "&timestamp=" + new Date().getTime(), function (data1) {
            if (data1.successful) {
                $.messager.show({
                    title: '提示信息',
                    msg: data1.serverMessage
                });
            } else {
                $.messager.show({
                    title: '提示信息',
                    msg: '批量更新新闻失败！ ' + data1.errorMessage
                });
            }

            $.messager.progress('close');
        });
    });
}

function beginCopyNews() {
    var number = $('input[name=number]').val();
    var newsId = $('input[name=copyNewsId]').val();

    closeNumberDialog();
    $.messager.progress({
        title: '拷贝新闻',
        msg: '拷贝新闻......！'
    });

    $.getJSON("/management/news/copyNews.action?number=" + number + "&id=" + newsId + "&timestamp=" + new Date().getTime(), function (data1) {
        if (data1.successful) {
            $.messager.show({
                title: '提示信息',
                msg: data1.serverMessage
            });
        } else {
            $.messager.show({
                title: '提示信息',
                msg: '拷贝新闻失败！ ' + data1.errorMessage
            });
        }

        $.messager.progress('close');
        query();
    });

}

function showMonthSelection() {
    $('#itemEditorDialog').show();
    $('#itemEditorDialog').dialog({
        title: '选择月份',
        modal: true
    });
}

function closeNumberDialog(){
    $('#numberDialog').dialog('close');
}

function closeMonthSelection(){
    $('#itemEditorDialog').dialog('close');
}

function showNews() {
    var rows = $('#newsGrid').datagrid('getSelections');
    var row = $('#newsGrid').datagrid('getSelected');
    if (rows.length == 1) {
        window.open("/management/news/showNews.action?id=" + row.id);
    } else if (rows.length < 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
    }
    else {
        $.messager.alert('警告', '一次只能查看一条记录!', 'warning');
    }
}

function openAboutNews() {
    var rows = $('#newsGrid').datagrid('getSelections');
    var row = $('#newsGrid').datagrid('getSelected');
    if (rows.length == 1) {
        window.location = "aboutNews.action?id=" + row.id;
    } else if (rows.length < 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
    }
    else {
        $.messager.alert('警告', '一次只能选择一条记录!', 'warning');
    }
}

function openUpdateNews() {
    var rows = $('#newsGrid').datagrid('getSelections');
    var row = $('#newsGrid').datagrid('getSelected');
    if (rows.length == 1) {
        window.location = "newsForm.action?id=" + row.id;
    } else if (rows.length < 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
    }
    else {
        $.messager.alert('警告', '一次只能修改一条记录!', 'warning');
    }
}

function deleteHtml(id) {
    $.messager.confirm('删除静态新闻', '你确定要删除静态新闻吗？', function (data) {
        if (!data) {
            return;
        }

        $.messager.progress({
            title: '静态新闻',
            msg: '删除静态新闻......！'
        });

        $.getJSON("/management/news/deleteHtml.action?id=" + id + "&timestamp=" + new Date().getTime(), function (data1) {
            if (data1.successful) {
                $.messager.show({
                    title: '提示信息',
                    msg: '删除静态新闻成功！'
                });
            } else {
                $.messager.show({
                    title: '提示信息',
                    msg: '删除静态新闻失败！ ' + data1.errorMessage
                });
            }

            $.messager.progress('close');
        });
    });
}

function saveHtml(id) {
    $.messager.progress({
        title: '静态新闻',
        msg: '保存静态新闻......！'
    });

    $.getJSON("/management/news/saveNewsToHtml.action?id=" + id + "&timestamp=" + new Date().getTime(), function (data1) {
        if (data1.successful) {
            $.messager.show({
                title: '提示信息',
                msg: '保存静态新闻成功！'
            });
        } else {
            $.messager.show({
                title: '提示信息',
                msg: '保存静态新闻失败！ ' + data1.errorMessage
            });
        }

        $.messager.progress('close');
    });
}

function copyNews(id) {
    $('#numberDialog').show();
    $('#copyNewsId').val(id);
    $('#numberDialog').dialog({
        title: '拷贝新闻',
        modal: true
    });
}

function deleteNews(id, index) {
    $.messager.confirm('删除新闻', '你确定要删除新闻吗？', function (data) {
        if (!data) {
            return;
        }

        $.getJSON("/management/news/deleteNews.action?id=" + id + "&timestamp=" + new Date().getTime(), function (data1) {
            if (data1.successful) {
                $.messager.show({
                    title: '提示信息',
                    msg: '删除新闻成功！'
                });

                var index = $("#newsGrid").datagrid('getRowIndex', id);
                $('#newsGrid').datagrid('deleteRow', index);
            } else {
                $.messager.show({
                    title: '提示信息',
                    msg: '删除新闻失败！'
                });
            }
        });
    });
}

function query() {
    var newsTitle = $("#newsTitle").val();
    var newsIndexTitle = $("#newsIndexTitle").val();
    var newsColumn = $("input[name='newsColumn']").val();
    var inIndex = $("input[name='inIndex']").val();
    var indexLevel = $("input[name='indexLevel']").val();
    var queryParams = $('#newsGrid').datagrid('options').queryParams;

    queryParams.newsTitle = newsTitle;
    queryParams.newsIndexTitle = newsIndexTitle;
    queryParams.newsColumn = newsColumn;
    queryParams.inIndex = inIndex;
    queryParams.indexLevel = indexLevel;

    $('#newsGrid').datagrid('options').queryParams = queryParams;
    $("#newsGrid").datagrid('reload');
}

function cleanColumn() {
    $("#columnCombo").combotree('setValue', '');
}

function getSelected() {
    var selected = $("#newsGrid").datagrid("getSelected");
    return selected;
}

function getSelections() {
    var rows = $("#newsGrid").datagrid("getSelections");
    return rows;
}

function selectRow(rowNumber) {
    $("#newsGrid").datagrid("selectRow", rowNumber);
}

</script>

</head>

<body class="easyui-layout">
<s:if test="null == #session._login_user_">
    <script type="text/javascript">
        top.location.href = '/admin';
    </script>
</s:if>
<div region="center" style="height:600px;padding:10px;background:#efefef;">
    <div id="p" class="easyui-panel" title="新闻搜索" icon="icon-search" style="height:80px;padding:10px;">
        <table style="border: #15428B; border-style: solid; border-width: 1px ">
            <tr>
                <td><label>标题:</label></td>
                <td>&nbsp;<input name="newsTitle" id="newsTitle"/></td>
                <td><label>首页标题:</label></td>
                <td>&nbsp;<input name="newsIndexTitle" id="newsIndexTitle"/></td>
                <td><label>栏目:</label></td>
                <td>&nbsp;<select id="columnCombo" class="easyui-combotree" name="newsColumn"
                                  url="/management/columns/fetchColumnsForTree.action"
                                  cascadeCheck="false" style="width:200px;" panelHeight="400"></select>&nbsp;</td>
                <td><a class="easyui-linkbutton" onclick="cleanColumn()">清除栏目</a>&nbsp;&nbsp;</td>
                <td><label>置顶:</label></td>
                <td>&nbsp;
                    <select id="inIndex" class="easyui-combobox" name="inIndex" style="width:60px;">
                        <option value="-1">所有</option>
                        <option value="0">不推荐</option>
                        <option value="1">频道页</option>
                        <option value="2">首页</option>
                    </select>&nbsp;
                </td>
                <td><label>推荐:</label></td>
                <td>
                    &nbsp; <select id="indexLevel" class="easyui-combobox" name="indexLevel" style="width:60px;">
                    <option value="-1">所有</option>
                    <option value="1">推荐1</option>
                    <option value="2">推荐2</option>
                    <option value="3">推荐3</option>
                    <option value="4">推荐4</option>
                    <option value="5">推荐5</option>
                </select>&nbsp;
                </td>
                <td style="width:20px"></td>
                <td><a class="easyui-linkbutton" onclick="query()">搜索</a></td>
            </tr>
        </table>
    </div>
    <div id="p1" class="easyui-panel" style="height:500px;">`
        <table id="newsGrid"></table>
    </div>
</div>

<div id="itemEditorDialog" icon="icon-save" title="栏目管理" style="height:200;width:260;display:none;padding:5px;">
        <table>
            <tr>
                <td><label>栏目名称：</label></td>
                <td><select id="monthSelection" class="easyui-combobox" name="monthSelection"
                            style="width:200px;"
                            required="true">
                    <option value="1">更新本月新闻</option>
                    <option value="2">更新最近二个月新闻</option>
                    <option value="3">更新最近三个月新闻</option>
                </select>
                </td>
            </tr>
        </table>
        <div class="toolbar" style="padding:10px;">
            <a href="#" onclick="refreshHtmlNews()" class="easyui-linkbutton" iconCls="icon-ok"
               style="float: right;">确定</a>
            <a href="#" onclick="closeMonthSelection()" class="easyui-linkbutton" iconCls="icon-cancel"
               style="float: right;">取消</a>
        </div>
</div>

<div id="numberDialog" icon="icon-save" title="栏目管理" style="height:200;width:260;display:none;padding:5px;">
    <table>
        <tr>
            <td><label>拷贝数量：</label></td>
            <td><input name="number" id="number" type="text" class="easyui-numberbox" precision="0" max="20" min="1" size="8" maxlength="2"  style="text-align:right;"/>
            </td>
            <td><input name="copyNewsId" id="copyNewsId" type="hidden"/></td>
        </tr>
    </table>
    <div class="toolbar" style="padding:10px;">
        <a href="#" onclick="beginCopyNews()" class="easyui-linkbutton" iconCls="icon-ok"
           style="float: right;">确定</a>
        <a href="#" onclick="closeNumberDialog()" class="easyui-linkbutton" iconCls="icon-cancel"
           style="float: right;">取消</a>
    </div>
</div>

</body>
</html>